import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.rmi.registry.*;


public class DoubleReferencer extends Frame implements WindowListener {
	public TextArea		text;
	public TextField	data;
	Dublet_itf dublet;
	Sentence_itf		sentence;
	static String		myName;

	public static void main(String argv[]) {
		
		if (argv.length != 1) {
			System.out.println("java Irc <name>");
			return;
		}
		myName = argv[0];
	
		// initialize the system
		Client.init();
		
		// look up the IRC object in the name server
		// if not found, create it, and register it in the name server
		Dublet_itf dbl = (Dublet_itf)Client.lookup("DoubleRefr");
		if (dbl == null) {
			dbl = (Dublet_itf)Client.create(new Dublet()); 
			Client.register("DoubleRefr", dbl);
		}
		// create the graphical part
		new DoubleReferencer(dbl);
	}

	public DoubleReferencer(Dublet_itf s) {
	
		setLayout(new FlowLayout());
	
		text=new TextArea(10,60);
		text.setEditable(false);
		text.setForeground(Color.red);
		add(text);
	
		data=new TextField(55);
		add(data);
	
		dublet = s;
		
		Button initSentenceButton = new Button("initS");
		initSentenceButton.addActionListener(new initSListenerDR(this));
		add(initSentenceButton);
		
		Button fetchSentenceButton = new Button("fetchS");
		fetchSentenceButton.addActionListener(new fetchSListenerDR(this));
		add(fetchSentenceButton);
		
		Button write_button = new Button("write");
		write_button.addActionListener(new writeListenerDR(this));
		add(write_button);
		
		Button read_button = new Button("read");
		read_button.addActionListener(new readListenerDR(this));
		add(read_button);
		
		
		setSize(500,300);
		text.setBackground(Color.black); 
		this.addWindowListener(this);
		setVisible(true);
		
	}

	
	
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}



class initSListenerDR implements ActionListener{
	DoubleReferencer dbl;
	public initSListenerDR(DoubleReferencer dbl){
		this.dbl = dbl;
	}
	public void actionPerformed(ActionEvent e){
		Sentence_itf sentence = (Sentence_itf)Client.create(new Sentence());
		dbl.dublet.lock_write();
		dbl.dublet.setSharo(sentence);
		dbl.dublet.unlock();
		
		dbl.sentence = sentence;
	}
}

class fetchSListenerDR implements ActionListener{
	DoubleReferencer dbl;
	public fetchSListenerDR(DoubleReferencer dbl){
		this.dbl = dbl;
	}
	public void actionPerformed(ActionEvent e){
		dbl.dublet.lock_read();
		dbl.sentence = (Sentence_itf) dbl.dublet.getSharo();
		dbl.dublet.unlock();
	}
}

class readListenerDR implements ActionListener {
	DoubleReferencer irc;
	public readListenerDR(DoubleReferencer i) {
		irc = i;
	}
	public void actionPerformed (ActionEvent e) {
		
		// lock the object in read mode
		irc.sentence.lock_read();
		
		// invoke the method
		String s = irc.sentence.read();
		
		// unlock the object
		irc.sentence.unlock();
		
		// display the read value
		irc.text.append(s+"\n");
	}
}

class writeListenerDR implements ActionListener {
	DoubleReferencer irc;
	public writeListenerDR(DoubleReferencer i) {
        	irc = i;
	}
	public void actionPerformed (ActionEvent e) {
		
		// get the value to be written from the buffer
        	String s = irc.data.getText();
        	
        // lock the object in write mode
        irc.dublet.lock_write();	
		irc.sentence.lock_write();
		
		// invoke the method
		irc.sentence.write(DoubleReferencer.myName+" wrote : "+s);
		irc.data.setText("");
		
		// unlock the object
		irc.sentence.unlock();
		irc.dublet.unlock();
	}
}